package com.zl.learning.thread;

import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

/**
 * @author tengfei.fangtf
 * @version $Id: CountDownLatchTest.java, v 0.1 2015-8-1 上午12:08:52 tengfei.fangtf Exp $
 */
public class CountDownLatchTest {

    private static CountDownLatch c = new CountDownLatch(2);

    public static void main(String[] args) throws InterruptedException {
        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(1);
            c.countDown();
        }).start();

        new Thread(() -> {
            try {
                TimeUnit.SECONDS.sleep(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            System.out.println(2);
            c.countDown();
        }).start();

        c.await();
        System.out.println("3");
    }

    public static class Test2 {
        public static void main(String[] args) throws InterruptedException {
            CountDownLatch countDownLatch = new CountDownLatch(8);
            for (int i = 1; i < 9; i++) {
                new Thread(() -> {
                    System.out.println(Thread.currentThread().getName() + "\t 号同学离开教室");
                    countDownLatch.countDown();
                }, String.valueOf(i)).start();
            }
            countDownLatch.await();
            System.out.println(Thread.currentThread().getName() + "\t 班长离开教室锁门");
        }
    }
}
